<template>
    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
      <div class="widget am-cf">
        <div class="widget-head am-cf">
          <div class="widget-title am-fl">学位预约统计</div>
        </div>
        <div class="widget-body  am-fr degree-subscribe">
            <div class="am-u-sm-12 am-form ">
                <el-select  size="small" class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group"
                     @change="handleAreaTeamIdChange" v-model="formData.areaTeamId" placeholder="请选择区域">
                    <el-option
                        v-for="item in areaTeams"
                        :key="item.areaTeamId"
                        :label="item.name"
                        :value="item.areaTeamId">
                    </el-option>
                </el-select>
                <el-select  size="small" class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group"
                        v-model="formData.busTeamId" placeholder="请选择业务组">
                    <el-option value="">请选择业务组</el-option>
                    <el-option
                        v-for="item in busTeams"
                        :key="item.busTeamId"
                        :label="item.name"
                        :value="item.busTeamId">
                    </el-option>
                </el-select>
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group" >
                    <el-date-picker
                        v-model="year"
                        type="year"
                        :clearable="false"
                        :editable="false"
                        placeholder="请选择年份">
                    </el-date-picker>
                </div>
                <el-select  size="small" class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group" 
                      v-model="formData.periodIds" placeholder="请选择期数">
                    <el-option value="">请选择期数</el-option>
                    <el-option
                        v-for="item in periods"
                        :key="item.periodId"
                        :label="item.periodName"
                        :value="item.periodId">
                    </el-option>
                </el-select>
                <el-select size="small" class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group" 
                          v-model="formData.gradeId" placeholder="请选择年级">
                  <el-option value="">请选择年级</el-option>
                  <el-option
                    v-for="item in grades"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
                 <el-select  size="small" class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group" 
                          v-model="formData.followStatus" placeholder="是否已跟进">
                  <el-option value="">请选择是否已跟进</el-option>
                  <el-option
                    v-for="item in status"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group">
                 
                    <el-date-picker
                      v-model="submitTime"
                      type="daterange"
                      placeholder="提交日期">
                    </el-date-picker>
                  
                </div>
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group" >
                  <el-input v-model="formData.className" placeholder="班级名称"></el-input>
                </div>
               <div class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group am-u-end">
                <button @click="handleSearch" type="button" class="btn-search am-btn am-btn-default am-btn-success">
                  <span class="am-icon-search"></span>查询
                </button>
              </div>
            </div>
          <div class="am-u-sm-12">
            <el-table
              :data="tableData"
              border
              stripe
              style="min-width: 100%">
              <el-table-column
                prop="periodName"
                label="期数"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="busTeamName"
                label="业务组"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="gradeName"
                label="年级"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="subjectName"
                label="科目"
                min-width="100">
              </el-table-column>
               <el-table-column
                prop="className"
                label="班级"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="level"
                label="层级"
                min-width="100">
                <template scope="scope">
                  <span>{{scope.row.level | getRegLevelVal}}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="phoneNo"
                label="预约手机号"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="remark"
                label="备注"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="phoneNo"
                label="提交时间"
                min-width="100">
                <template scope="scope">
                  <span>{{scope.row.createTime | formatTime}}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="followName"
                label="跟进人"
                min-width="100">
              </el-table-column>
              <el-table-column
                align="center"
                label="操作"
                min-width="100">
                <template scope="scope">
                    <div >
                      <el-button size="small" class="done" :disabled="true" v-if="scope.row.followStatus==1">已跟进</el-button>
                      <el-button size="small"  @click="followUp(scope.row)" v-if="scope.row.followStatus==0"> 跟进</el-button>
                    </div>
                </template>
              </el-table-column>
            </el-table>
          </div>  
        </div>
        <div class="am-u-lg-12 am-cf">
          <div class="am-fr">
            <pagination v-bind:total="total" v-bind:pageNo="pageNo" v-bind:pageSize="pageSize"
                        @paging="loadTableData"/>
          </div>
        </div>
      </div>
    </div>
</template>
<script>
import moment from 'moment'
import io from '../../lib/io'
import util from '../../lib/util'
import storage from '../../lib/storage'
import Pagination from '../base/Pagination'
export default {
    components:{
      Pagination
    },
    data(){
        return{
            periods:[],
            submitTime:[],
            year:"",
            status:[
              {
                label:"是",
                value:1,
              },
              {
                label:"否",
                value:0,
              }
            ],
            formData:{
                areaTeamId:window.config.areaTeams[0] && window.config.areaTeams[0].areaTeamId || '' ,
                busTeamId:'',
                year:"",
                periodIds:"",
                gradeId:"",
                followStatus:"",
                className:"",
            },
            periodIds:"",
            tableData:[],
            total:0,
            pageNo:1,
            pageSize:10,
        }
    },
    computed: {
      areaTeams: function () {
        this.loadPeriodByYear()
        return this.$root.config.areaTeams || []
      },
      busTeams: function () {
        return ( ( this.formData.areaTeamId  ) ? ( this.$root.config.groupBusTeams[this.formData.areaTeamId] || [] ) : [] )
      },
      grades () {
        return this.$root.config.grades.map(function(item){
          return {value: item.gradeId, label: item.gradeName}
        })
      },
    },
    created(){
      this.year = (storage.getMultipleToolbar()&&storage.getMultipleToolbar().year) || moment(new Date()).format('YYYY')
      let promise = new Promise((resolve, reject) => {
          this.loadPeriodByYear(resolve)
      });
      promise.then((value)=>{
        this.loadTableData()
      })
      
    },
    watch:{
      year(newVal, oldVal) {
        if (newVal !== oldVal) {
          this.loadPeriodByYear()
        }
      }
    },
    mounted: function () {
			$(window).smoothScroll()
		},
    methods:{
        loadPeriodByYear(resolve) {
          io.post(io.periodByYearAndAreaTeamId, {
            areaTeamId : this.formData.areaTeamId,
            year: this.year ? moment(this.year).format('YYYY'): '',
          }, (ret) => {
            if (ret.success) {
              this.periods = ret.data;
              console.log(ret.data)
              let curPeriodId = ret.data.filter(item => item.isCurrent == 1 )[0] ? ret.data.filter(item => item.isCurrent == 1 )[0].periodId : '';
              if(curPeriodId != ''){
                this.formData.periodIds = curPeriodId
              }else{
                this.formData.periodIds = this.periods.length ? this.periods[0].periodId : ''
              }
              console.log(ret.data,curPeriodId)
              console.log(this.formData.periodIds)
            } else {
              this.$alert(ret.desc)
            }
            if(resolve){
                resolve('success');
            }
          })
	      },
        handleAreaTeamIdChange(){

        },
        handleSearch(){
          this.pageNo = 1
          if(this.formData.periodIds===''){
            this.periodIds = this.periods.map((item)=>{return item.periodId}).join(',')
           
          }else{
            this.periodIds = this.formData.periodIds
          }
          if(this.periodIds===''){
            this.tableData = []
            this.$alert('该年份无期数，请前往设置期数')
            return
          }
          this.loadTableData()
        },
        loadTableData(pageNo){
          console.log(pageNo)
          let param = {...this.formData}
          param.year = moment(this.year).format('YYYY')
          param.startDate = util.formatDate(this.submitTime[0])
          param.endDate = util.formatDate(this.submitTime[1])
          param.pageNo = pageNo||this.pageNo||1
          param.pageSize = this.pageSize
          param.periodIds = this.periodIds
          io.post(io.findDegreeSubscribeDetailPage,param,(ret)=>{
            if(ret.success){
              this.total = ret.data.total
              this.pageNo = ret.data.pageIndex
              this.tableData = ret.data.list
            }else {
              this.$alert(ret.desc)
            }
          })
        },
        followUp(row){
          let param = {
            degreeSubscribeId:row.degreeSubscribeId
          }
          io.post(io.followDegreeSubscribe,param,(ret)=>{
            if(ret.success){
              this.loadTableData()
            }else{
              this.$alert(ret.desc)
            }
          })
        }
    }
}
</script>
<style lang="less" scoped>

</style>
<style lang="less">
.degree-subscribe{
  .am-form{
    input[type=text][readonly] {
      cursor: text;
      background-color: transparent;
    }
    
  }
}
</style>

